--[[
游戏规则
1.3个人玩
2.玩法1,除去大王，小王，红桃2A，草花2A，方片2A，黑桃K，每人15张
  玩法2,除去大王，小王，红桃2，草花2，方片2，黑桃A，每人16张

单张：可以是手中的任意一张牌
对子：两张牌点相同的牌，两张牌的花色可以不同
连对：两对或两对以上相连的牌，如：5566
三带二：三张牌点相同的牌，带二张杂牌，如：55566、55567
飞机带翅膀：两个或两个以上相连的三同张牌，如：5556667788，QQQKKK8899
顺子：五张或五张以上牌点连续的牌。例如：3456789,10JQKA等
炸弹：四张或四张以上牌点相同的牌，如：6666、8888

单张大小：2 > A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3
同牌型间：同种牌型比较点数大小，相同牌型则出牌数量必须一致
不同牌型：除炸弹可以炸任何牌型，其余不同牌型互不相压
]]

local random = math.random
local table_copy = table.copy
local floor = math.floor
local modf = math.modf
local log = require("Log")

local Rule = class("Rule")

function  Rule:ctor()
	--随机种子
	math.randomseed(os.time())
	--45张牌 A=14,2=15,小王=160,大王=170
	self.card1 = {
		31,41,51,61,71,81,91,101,111,121,131,
		32,42,52,62,72,82,92,102,112,122,132,
		33,43,53,63,73,83,93,103,113,123,133,
		34,44,54,64,74,84,94,104,114,124,144,154,
	}
	--48张牌
	self.card2 = {
		31,41,51,61,71,81,91,101,111,121,131,141,
		32,42,52,62,72,82,92,102,112,122,132,142,
		33,43,53,63,73,83,93,103,113,123,133,143,
		34,44,54,64,74,84,94,104,114,124,134,154,
	}	
	self.card = self.card1

	--要发的牌
	self.tbCard = {}
	self.cardPairs = 1	--有几副牌
	self.ignoreCard = {160,170,141,142,143} --忽略的牌
	--牌型
  	self.cardType = {
	  	danZhang 	= 1, --单张
	  	duiZi 		= 2,
	  	sanZhang 	= 3,
	  	sanDaiYi 	= 4,	  	
	  	sanDaiEr 	= 5,
	  	liangDui 	= 6,	  	
	  	shunZi 		= 7,
	  	zhaDan 		= 8,
	  	siDaiN      = 9,
	  	feiJi   	= 10,

	  	-- wangZha 	= 12,
	}
			
end

--洗牌
function Rule:shuffle(cardNum)	
	
	-- print("___22222222_________self.card__",self.card)
	self.card = self.card1	
	if cardNum == 16 then 
		self.card = self.card2
	end	
	self.tbCard = {}
	for _, v in pairs(self.card) do 
		table.insert(self.tbCard, v)
	end
	table.mix(self.tbCard)
	-- if cardNum == 15 then 
	-- 	self.tbCard = {
	-- 		31,32,33,34,71,91,101,64,131,133,41,42,43,72,103,
	-- 		82,92,102,132,51,52,53,61,62,63,44,81,54,83,84,
	-- 		113,111,123,121,74,94,104,114,124,144,154,73,93,112,122,
	-- 	}	
	-- end
end

--取指定个数的牌
function Rule:getCard(num)
	local tbRes = {}
	local n = num or 0
	for k,v in pairs(self.tbCard) do 
		if n <= 0 then break end
		if v then 
			n = n-1
			table.insert(tbRes,v)
			self.tbCard[k] = nil
		end
	end
	return tbRes
end

--取牌值
function Rule:getCardNum(card)
	return floor(card / 10)
end

--取牌色
function Rule:getCardColor(card)
	return card % 10
end

--取牌
function Rule:getPlayAndBankCards()	

end

--输出牌型
function Rule:printCardType(cardType)
	for k,v in pairs(self.cardType) do 
		if v == cardType then 
			print("牌型"..k)
			return
		end
	end
end

--对牌排序
function Rule:sortCards(tbCard)
	local n1,n2
	local c1,c2
	local function sortFun(a,b)
		n1 = self:getCardNum(a)
		n2 = self:getCardNum(b)
		c1 = self:getCardColor(a)
		c2 = self:getCardColor(b)
		if n1 > n2 then 
			return true
		elseif n1 == n2 then 
			if c1 > c2 then 
				return true
			end
		end
		return false
	end
	table.sort(tbCard,sortFun)	
end

--取出所有连张
--num 连牌数
function Rule:getAllSame(tbCard,num)
	self:sortCards(tbCard)	
	local len = #tbCard
	local tbRes = {}
	local tb = {}
	local num1 
	local num2 
	local n = 0
	for i=1,len-1 do 
		if not num1 then
			num1 = self:getCardNum(tbCard[i])
			table.insert(tb,tbCard[i])
		end
		num2 = self:getCardNum(tbCard[i+1])
		if num1 == num2 then 
			n = n + 1
			table.insert(tb,tbCard[i+1])
		else
			tb = {}
			num1 = nil
			n = 0
		end
		if n == num-1 then 			
			table.insert(tbRes,tb)
			tb = {}
			n = 0
			num1 = nil
		end
	end	
	return tbRes
end

------------------牌特征码 给客户端用的
--[[
  根据一组合法牌计算特征值，用来比较大小
  特征值结构为
  【牌型大类（两位） | 节数（两位） | 牌值（三位）| 预留一位（默认4,为1则代表包含癞子）】
  比如55566637特征值为31 02 005 0。31表示三顺带单，02表示两节（从5到6），005是取最小那节的牌值，4是默认值。

  牌型代码和例子：
  单张。1。单张A2K的值依次是1 01 014 4，1 01 015 4，1 01 013 4。
  对子。2。对7 = 2 01 007 4。
  三条。3。三个5 = 3 01 005 4。
  三带单。4。5557 = 4 01 005 4。
  三带对。5。55577 = 5 01 005 4。
  四带n(1-3)。8。555579 = 8 01 005 4。
  四带两对。9。55557799 = 9 01 005 4。

  单顺。21。456789 = 21 06 004 4。
  双顺。22。556677 = 22 03 005 4。
 
  三顺带n(1-4)。飞机。24。66677735 = 24 02 006 4。
  
  四条。炸弹。41。JJJJ = 41 01 011 4。

]]
function Rule:calculateValue(a, b, c, fix)
  local fixValue = fix or 4
  return a * 1000000 + b * 10000 + c * 10 + fixValue
end

-- 分解特征值
function Rule:explodeValue(value)
  local a, b, c
  a, value =  math.modf(value / 1000000)
  value = value * 100000
  b, value = math.modf(value  / 1000)
  value = value * 1000
  c = math.modf(value)
  return a, b, c
end

--客户端需要的一个值
function Rule:getCardCode(tbCard)
	if not tbCard or not next(tbCard) then 
		return 
	end
  	local codeType = {
	  	danZhang 	= 1, --单张
	  	duiZi 		= 2,
	  	sanZhang 	= 3,
	  	sanDaiYi 	= 4,
	  	sanDaiEr 	= 5,
	  	siDaiN 		= 8,
	  	liangDui 	= 22,
	  	feiJi 		= 24,
	  	shunZi 		= 21,
	  	zhaDan 		= 41,
	}
	local codeNum,minCard
	local len = #tbCard
	local a,b,c
	local fix = 4
	local cardType
	local n --飞机节数
	b = 1
	c = self:getCardNum(tbCard[1])	
	cardType,minCard,n = self:getCardType(tbCard)	
	-- print("_____cardType__",cardType,minCard)
	if cardType == self.cardType.danZhang then 
		a = codeType.danZhang		
	elseif cardType == self.cardType.duiZi then
		a = codeType.duiZi
	elseif cardType == self.cardType.sanZhang then		
		a = codeType.sanZhang
	elseif cardType == self.cardType.siDaiN then		
		a = codeType.siDaiN		
		c = minCard
	elseif cardType == self.cardType.sanDaiYi then 	
		a = codeType.sanDaiYi	
		c = minCard	
 	elseif cardType == self.cardType.sanDaiEr then		
		a = codeType.sanDaiEr	
		c = minCard	 		
	elseif cardType == self.cardType.liangDui then	
		a = codeType.liangDui
		b = floor(len/2)
		c = self:getCardNum(tbCard[len])		
	elseif cardType == self.cardType.shunZi then
		a = codeType.shunZi
		b = len
		c = self:getCardNum(tbCard[len])			
	elseif cardType == self.cardType.zhaDan then
		a = codeType.zhaDan			
	elseif cardType == self.cardType.feiJi then
		a = codeType.feiJi		
		b = n
		c = minCard		
	end
	if a then 
		-- print("____(a,b,c,fix)__",a,b,c,fix)
		codeNum = self:calculateValue(a,b,c,fix)
	end
	-- print("___codeNum___",codeNum)
	return codeNum,cardType
end

------------------牌型------------------
--单张
function Rule:isDanZhang(tbCard)
	if #tbCard == 1 then 
		return true
	end
	return false
end

--对子 
function Rule:isDuizi(tbCard)
	if #tbCard ~= 2 then 
		return false
	end
	for k, v in ipairs(tbCard) do
		for j = k + 1, #tbCard do
			if self:getCardNum(tbCard[k]) == self:getCardNum(tbCard[j]) then
				return true
			end
		end
	end
	return false
end

--连对
function Rule:isLiangDui(tbCard)	
	local len = #tbCard
	if len < 4 or len%2 ~= 0 then 
		return false
	end
	self:sortCards(tbCard)
	local tb = self:getAllSame(tbCard,2)
	if #tb ~= len/2 then 
		return false
	end	
	for i=1, #tb-1, 1 do
		if self:getCardNum(tb[i][1]) ~= self:getCardNum(tb[i+1][1])+1 then
			return false			
		end
	end	
	if next(tb) and #tb >= 2 then 
		return true
	end
	return false
end

--3不带
function Rule:isSanZhang(tbCard)
	local len = #tbCard
	if len ~= 3 then 
		return false
	end
	local num1 = self:getCardNum(tbCard[1])
	if num1 == self:getCardNum(tbCard[2]) and num1 == self:getCardNum(tbCard[3]) then 
		return true
	end
	return false
end

--3带1
function Rule:isSanDaiYi(tbCard)
	local len = #tbCard
	local num1,num2,num3,num4
	if len ~= 4 then 
		return false
	end
	self:sortCards(tbCard)
	for i=1,2 do 
		num1 = self:getCardNum(tbCard[i])
		num2 = self:getCardNum(tbCard[i+1])
		num3 = self:getCardNum(tbCard[i+2])		
		if num1 == num2 and num1 == num3 then 
			return true,num1
		end
	end
	return false
end

--3带2
function Rule:isSanDaiEr(tbCard)
	local len = #tbCard
	local num1,num2,num3,num4
	if len ~= 5 then 
		return false
	end
	self:sortCards(tbCard)
	for i=1,3 do 
		num1 = self:getCardNum(tbCard[i])
		num2 = self:getCardNum(tbCard[i+1])
		num3 = self:getCardNum(tbCard[i+2])		
		if num1 == num2 and num1 == num3 then 
			return true,num1
		end
	end
	return false
end

--4带n(1-3)
function Rule:isSiDaiN(tbCard)
	local len = #tbCard
	local num1,num2,num3,num4
	if len < 5 or len > 7 then 
		return false
	end
	self:sortCards(tbCard)
	for i=1,len-3 do 
		num1 = self:getCardNum(tbCard[i])
		num2 = self:getCardNum(tbCard[i+1])
		num3 = self:getCardNum(tbCard[i+2])
		num4 = self:getCardNum(tbCard[i+3])
		if num1 == num2 and num1 == num3 and num1 == num4 then 
			return true,num1
		end
	end
	return false
end


--炸弹
function Rule:isZhaDan(tbCard)
	local len = #tbCard
	if len ~= 4 then
		return false
	end
	self:sortCards(tbCard)
	local num1 = self:getCardNum(tbCard[1])
	local num2 = self:getCardNum(tbCard[2])
	local num3 = self:getCardNum(tbCard[3])
	local num4 = self:getCardNum(tbCard[4])
	if num1 == num2 and num1 == num3 and num1 == num4 then
		return true
	end	
	return false
end

--王炸
function Rule:isWangZha(tbCard)
	if #tbCard ~= 2 then 
		return false
	end
	if tbCard[1] + tbCard[2] == 160+170 then 
		return true
	end
	return false
end

--飞机不带
function Rule:isFeiJiBuDai(tbCard)
 	local len = #tbCard
    local n = floor(len / 3)
    local tbCardNum = {}
    if len %3 ~= 0 then 
        return false
    end
    local tb
    for i=1, n do     	
    	tb = table.sub(tbCard,(i-1)*3+1,(i-1)*3+3)
    	if not self:isSanZhang(tb) then 
    		return false
    	else
    		--如果连续的3张牌是一样的，记录其中一张牌的值 
    		tbCardNum[i] = self:getCardNum(tbCard[(i-1)*3+1])
    	end
    end
    for i=1,n-1 do 
    	if tbCardNum[i] - tbCardNum[i+1] ~= 1 then
    		--牌值不连接
    		print("牌值不连接")
    		return false
    	end
    end	
    return true,tbCardNum[#tbCardNum],n
end

--飞机带
function Rule:isFeiJiDai(tbCard)
	local len = #tbCard
	local n = 1 --多少节
	local num1,num2,num3
	local minCard	
	if len < 7 then 
		return false
	end
	-- print("isFeiJiDai___",dump(tbCard))
	local tbSan = self:getAllSame(tbCard,3)
	if #tbSan < 2 then 
		return false
	end
	for i=1,#tbSan-1 do 
		num1 = self:getCardNum(tbSan[i][1])
		num2 = self:getCardNum(tbSan[i+1][1])
		if num1 == num2 + 1 then 
			n = n + 1
			minCard = num2
		end
	end
	if len > n*3+n*2 then 
		return false
	end
	-- print("_____n___",n)
	return true,minCard,n
end

--飞机
function Rule:isFeiJi(tbCard)
	local len = #tbCard
	local flag = false
	local minCard
	local n
	if len < 6 then 
		return false
	end
	self:sortCards(tbCard)
	flag,minCard,n = self:isFeiJiBuDai(tbCard)
	if not flag then 
		flag,minCard,n = self:isFeiJiDai(tbCard)
	end	
	return flag,minCard,n
end


--顺子
function Rule:isShunZi(tbCard)
	local len = #tbCard
	if len <5 or len > 12 then 
		return false
	end
	self:sortCards(tbCard)
	local prevNum,nextNum
	local tbIgnoreCard = {17,16,15}
	for i=1,len-1 do 
		nextNum = self:getCardNum(tbCard[i])
		prevNum = self:getCardNum(tbCard[i+1])
		for k,v in pairs(tbIgnoreCard) do 
			if prevNum == v or nextNum==v then 
				return false
			end
		end
		if nextNum - prevNum ~= 1 then 
			return false
		end
	end
	return true
end

--取牌型
function Rule:getCardType(tbCard)
	local cardType 
	local res,subType,minCard
	local n --节数
	self:sortCards(tbCard)
	if self:isDanZhang(tbCard) then 
		cardType = self.cardType.danZhang
	elseif self:isDuizi(tbCard) then 
		cardType = self.cardType.duiZi
	elseif self:isZhaDan(tbCard) then 
		cardType = self.cardType.zhaDan
	elseif self:isSanZhang(tbCard) then 
		cardType = self.cardType.sanZhang
	end
	if cardType then 
		return cardType
	end	
	res,minCard = self:isSiDaiN(tbCard)
	if res then 
		cardType = self.cardType.siDaiN	
		return cardType,minCard	
	end			
	res,minCard = self:isSanDaiEr(tbCard)
	if res then 
		cardType = self.cardType.sanDaiEr	
		return cardType,minCard	
	end		
	res,minCard = self:isSanDaiYi(tbCard)
	if res then 
		cardType = self.cardType.sanDaiYi	
		return cardType,minCard	
	end			
	if self:isLiangDui(tbCard) then 
		cardType = self.cardType.liangDui
	elseif self:isShunZi(tbCard) then 
		cardType = self.cardType.shunZi
	end
	if cardType then 
		return cardType
	end	
	res,minCard,n = self:isFeiJi(tbCard)
	if res then 
		cardType = self.cardType.feiJi
		return cardType,minCard,n
	end	
	if not cardType then 
		-- print("牌型不存在",dump(tbCard))
	end
	return cardType								
end

--比较单张牌
function Rule:compareDanZhang(card1,card2)
	local num1 = self:getCardNum(card1)
	local num2 = self:getCardNum(card2)
	if num1 > num2 then 
		return true
	elseif num1 < num2 then 
		return false
	end
	--花色大小
	local color1 = self:getCardColor(card1)
	local color2 = self:getCardColor(card2)
	if color1 > color2 then 
		return true
	end
	return false
end

--比较手牌 
--tbCard1 > tbCard2 返回true
function Rule:compareCards(tbCard1, tbCard2, cardType1, cardType2)
	if not tbCard1 or not next(tbCard1) then 
		return false
	end
 	if not tbCard2 or not next(tbCard2) then
 		return true
 	end
 	local minCard1,minCard2
 	if not cardType1 then 
		cardType1,minCard1 = self:getCardType(tbCard1)
	end
	if not cardType2 then 
		cardType2,minCard2 = self:getCardType(tbCard2)
	end
	local cardType = self.cardType
	local len1 = #tbCard1
	local len2 = #tbCard2
    --集中判断对方不是炸弹，我出炸弹的情况
    if cardType2 ~= cardType.zhaDan and cardType1 == cardType.zhaDan then
        return true
    elseif cardType2 == cardType.zhaDan and cardType1 ~= cardType.zhaDan then
    	return false
    end
    self:sortCards(tbCard1)
    self:sortCards(tbCard2)
        
    local bCompareNum = self:compareDanZhang(tbCard1[1],tbCard2[1])
	-- print("____cardType1___",cardType1,cardType2,minCard1,minCard2)
	if  cardType2 ~= cardType1 then 
		return false
	end
	if len1 ~= len2 then 
		return false
	end	
	for k,v in pairs(self.cardType) do 
		if cardType1 == v then
			if v == cardType.sanDaiYi or v == cardType.sanDaiEr or v == cardType.feiJi or v == cardType.siDaiN then 
				return minCard1>minCard2 
			else
				return bCompareNum		
			end
		end
	end
end


--别人出牌我的手牌里有没有能管住他的牌
function Rule:getBiggerCard(tbMyCard,tbOtherCard,otherCardType)
	if not otherCardType then 
		otherCardType = self:getCardType(tbOtherCard)
	end
	local cardType = self.cardType
	local myLen = #tbMyCard	
	local tbZd = {} 
	local tbResCards = {} --所有结果
	local tb
	if not otherCardType then 
		--牌型不存在
		print("tbOtherCard牌型不存在",tbOtherCard)
		return false
	end
	self:printCardType(otherCardType)
	-- if otherCardType == cardType.wangZha then 
	-- 	--跑得快没有王
	-- 	return false
	-- end
	self:sortCards(tbMyCard)
	self:sortCards(tbOtherCard)
	if otherCardType ~= cardType.zhaDan and myLen>=4 then 	
		--炸弹
		tbZd = self:getZhaDan(tbMyCard)
		if tbZd and next(tbZd) then 
			table.concatList(tbResCards,tbZd)
		end
	end
	if otherCardType == cardType.danZhang then 
		--单张
		tb = self:getDanZhang(tbMyCard,tbOtherCard)
	elseif otherCardType == cardType.duiZi then 
		--对子
		tb = self:getDuiZi(tbMyCard,tbOtherCard)	
	elseif otherCardType == cardType.sanZhang then 
		--3张
		tb = self:getSanZhang(tbMyCard,tbOtherCard)	
	elseif otherCardType == cardType.sanDaiYi then 
		tb = self:getSanDaiYi(tbMyCard,tbOtherCard)		
	elseif otherCardType == cardType.sanDaiEr then 
		--3带2
		tb = self:getSanDaiEr(tbMyCard,tbOtherCard)
	elseif otherCardType == cardType.zhaDan then 	
		--炸弹
		tb = self:getZhaDan(tbMyCard,tbOtherCard)	
	elseif otherCardType == cardType.shunZi then 	
		--顺子
		tb = self:getShunzi(tbMyCard,tbOtherCard)
	elseif otherCardType == cardType.liangDui then
		--连对
		tb = self:getLiangDui(tbMyCard,tbOtherCard)
	elseif otherCardType == cardType.feiJi  then
		--飞机
		tb = self:getFeiji(tbMyCard,tbOtherCard)
	end	
	-- print("_________tb____",dump(tb))
	if tb and next(tb) then 
		table.concatList(tbResCards,tb)
	end
	return tbResCards
end

--取飞机
function Rule:getFeiji(tbMyCard,tbOtherCard,otherLen)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 6 then 
		return false
	end		
	local otherCardNum1 = 0 --最小节牌
	local cardType,js
	if tbOtherCard then
		otherLen = #tbOtherCard
		if otherLen < 6 then return false end 
		-- otherCardNum1 = self:getCardNum(tbOtherCard[4])
		cardType,otherCardNum1,js = self:getCardType(tbOtherCard)
		print("____otherCardNum1____",otherCardNum1)
	end	
	local cards = {}
	local type1,minCard
	for i=myLen,otherLen,-1 do 
		cards = {}
		for j=1,otherLen do 
			table.insert(cards,tbMyCard[i-j+1])
		end
		type1,minCard = self:getCardType(cards)
		if type1 == self.cardType.feiJi then 
			if minCard > otherCardNum1 then 
				table.insert(tbResCards,cards)
			end
		end
	end		
	return tbResCards
end

--取连对
function Rule:getLiangDui(tbMyCard,tbOtherCard,otherLen)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 4 then 
		return false
	end		
	local otherCardNum1 = 0
	if not otherLen then 
		otherLen = 4
	end
	if tbOtherCard then
		otherLen = #tbOtherCard
		if otherLen < 4 then return false end 
		otherCardNum1 = self:getCardNum(tbOtherCard[1])
	end		
	if myLen < otherLen then 
		return false
	end	
	local cards = {}
	for i=myLen,otherLen,-1 do 
		cards = {}
		for j=1,otherLen do 
			table.insert(cards,tbMyCard[i-j+1])
		end
		local type1 = self:getCardType(cards)
		if type1 == self.cardType.liangDui then 
			if self:getCardNum(cards[1]) > otherCardNum1 then 
				table.insert(tbResCards,cards)
			end
		end
	end
	return tbResCards
end

--取顺子
function Rule:getShunzi(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 5 then 
		return false
	end		
	local otherCardNum1 = 0
	local otherLen = 5
	if tbOtherCard then
		otherLen = #tbOtherCard
		if otherLen < 5 then return false end 
		otherCardNum1 = self:getCardNum(tbOtherCard[1])
	end		
	--顺子
	if myLen < otherLen then 
		return false
	end	
	--一样值的牌先过滤
	local num1,num2
	local tbMyCard2 = {}
	for i=1,myLen-1 do 
		if not num1 then 
			num1 = self:getCardNum(tbMyCard[i])
			table.insert(tbMyCard2,tbMyCard[i])
		end
		num2 = self:getCardNum(tbMyCard[i+1])
		if num1 ~= num2 then 
			table.insert(tbMyCard2,tbMyCard[i+1])
			num1 = num2
		end
	end
	local myLen2 = #tbMyCard2
	if myLen2 < otherLen then 
		return false
	end
	local type1
	local cards
	for i=myLen2,otherLen,-1 do 
		cards = {}
		for j=1,otherLen do 
			table.insert(cards,tbMyCard2[i-j+1])
		end			
		type1 = self:getCardType(cards)
		if type1 == self.cardType.shunZi then 
			if self:getCardNum(cards[1]) > otherCardNum1 then 											
				table.insert(tbResCards,cards)
			end
		end
	end	
	return tbResCards
end

--取炸弹
function Rule:getZhaDan(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 4 then 
		return false
	end		
	local otherCardNum1 = 0
	if tbOtherCard then 
		otherCardNum1 = self:getCardNum(tbOtherCard[1])
	end	
	local myNum1,myNum2,myNum3,myNum4
	for i=myLen,4,-1 do 
		myNum1 = self:getCardNum(tbMyCard[i])
		myNum2 = self:getCardNum(tbMyCard[i-1])
		myNum3 = self:getCardNum(tbMyCard[i-2])
		myNum4 = self:getCardNum(tbMyCard[i-3])
		if myNum2 == myNum1 and myNum3 == myNum1 and myNum4 == myNum1 then 
			if myNum1 > otherCardNum1 then 				
				resCards = {}
				table.insert(resCards,tbMyCard[i])
				table.insert(resCards,tbMyCard[i-1])
				table.insert(resCards,tbMyCard[i-2])	
				table.insert(resCards,tbMyCard[i-3])	
				table.insert(tbResCards,resCards)			
			end
		end
	end		
	return tbResCards
end

--取单张
function Rule:getDanZhang(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 1 then 
		return false
	end		
	local otherCardNum1 = 0
	if tbOtherCard then 
		otherCardNum1 = self:getCardNum(tbOtherCard[1])
	end	
	for k,v in pairs(tbMyCard) do 
		if self:getCardNum(v) > otherCardNum1 then 
			resCards = {}
			table.insert(resCards,v)
			table.insert(tbResCards,resCards)
		end
	end
	return tbResCards
end

--取对子
function Rule:getDuiZi(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 2 then 
		return false
	end		
	local otherCardNum1 = 0
	if tbOtherCard then 
		otherCardNum1 = self:getCardNum(tbOtherCard[1])
	end	
	local myNum1,myNum2
	for i=myLen,2,-1 do 
		myNum1 = self:getCardNum(tbMyCard[i])
		myNum2 = self:getCardNum(tbMyCard[i-1])								
		if myNum2 == myNum1 then
			if myNum1 > otherCardNum1 then  
				resCards = {}
				table.insert(resCards,tbMyCard[i])
				table.insert(resCards,tbMyCard[i-1])
				table.insert(tbResCards,resCards)
			end
		end
	end	
	return tbResCards
end

--取3张
function Rule:getSanZhang(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 3 then 
		return false
	end	
	local otherCardNum1 = 0
	if tbOtherCard then 
		otherCardNum1 = self:getCardNum(tbOtherCard[1])
	end	
	local myNum1,myNum2,myNum3
	for i=myLen,3,-1 do 
		myNum1 = self:getCardNum(tbMyCard[i])
		myNum2 = self:getCardNum(tbMyCard[i-1])
		myNum3 = self:getCardNum(tbMyCard[i-2])
		if myNum2 == myNum1 and myNum3 == myNum1 then 
			if myNum1 > otherCardNum1 then
				resCards = {}
				table.insert(resCards,tbMyCard[i])
				table.insert(resCards,tbMyCard[i-1])
				table.insert(resCards,tbMyCard[i-2])
				table.insert(tbResCards,resCards)				
			end
		end
	end		
	return tbResCards
end

--取3带1的牌
function Rule:getSanDaiYi(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 4 then 
		return false
	end
	local otherCardNum1 = 0
	if tbOtherCard then 
		otherCardNum1 = self:getCardNum(tbOtherCard[3])
	end	
	local myNum1,myNum2,myNum3
	local restCard
	local tbDanPai 
	for i=myLen,3,-1 do 
		myNum1 = self:getCardNum(tbMyCard[i])
		myNum2 = self:getCardNum(tbMyCard[i-1])
		myNum3 = self:getCardNum(tbMyCard[i-2])
		if myNum2 == myNum1 and myNum3 == myNum1 then 	
			if myNum1 > otherCardNum1 then 			
				resCards = {}
				table.insert(resCards,tbMyCard[i])
				table.insert(resCards,tbMyCard[i-1])
				table.insert(resCards,tbMyCard[i-2])
				--从所有单牌中取出2张小的牌
				restCard = table.subRest(tbMyCard,i-2,3)
				tbDanPai = self:getDanPai(restCard,myNum1)
				table.insert(resCards,tbDanPai[1])
				table.insert(tbResCards,resCards)		
			end	
		end
	end		
	return tbResCards
end

--取3带2的牌
function Rule:getSanDaiEr(tbMyCard,tbOtherCard)
	local tbResCards = {}
	local resCards = {}
	local myLen = #tbMyCard
	if myLen < 5 then 
		return false
	end
	local otherCardNum1 = 0
	if tbOtherCard then 
		otherCardNum1 = self:getCardNum(tbOtherCard[3])
	end	
	local myNum1,myNum2,myNum3
	local restCard
	local tbDanPai 
	for i=myLen,3,-1 do 
		myNum1 = self:getCardNum(tbMyCard[i])
		myNum2 = self:getCardNum(tbMyCard[i-1])
		myNum3 = self:getCardNum(tbMyCard[i-2])
		if myNum2 == myNum1 and myNum3 == myNum1 then 	
			if myNum1 > otherCardNum1 then 			
				resCards = {}
				table.insert(resCards,tbMyCard[i])
				table.insert(resCards,tbMyCard[i-1])
				table.insert(resCards,tbMyCard[i-2])
				--从所有单牌中取出2张小的牌
				restCard = table.subRest(tbMyCard,i-2,3)
				tbDanPai = self:getDanPai(restCard,myNum1)
				table.insert(resCards,tbDanPai[1])
				table.insert(resCards,tbDanPai[2])
				table.insert(tbResCards,resCards)		
			end	
		end
	end		
	return tbResCards
end

--从手牌中选出一个牌型
function Rule:getCardByType(tbMyCard,myCardType)
	local tbCard 
	local tbCardScr = table.clone(tbMyCard)
	local tbZha
	self:sortCards(tbCardScr)
	for num=#tbCardScr,6,-1 do 
		tbCard = self:getFeiji(tbCardScr,nil,num)
		if tbCard and next(tbCard) then 
			-- print("___2__getCardByType___",tbCard[1])
			return tbCard[1]
		end
	end
	tbZha = self:getZhaDan(tbCardScr)
	if tbZha and next(tbZha) then 
		tbCardScr = table.removeSub(tbCardScr,tbZha[1])
	end	

	tbCard = self:getSanDaiEr(tbCardScr)
	if tbCard and next(tbCard) then 
		return tbCard[1]
	end	
	for num=10,4,-2 do 
		-- print("___num__",num)
		tbCard = self:getLiangDui(tbCardScr,nil,num)
		if tbCard and next(tbCard) then 
			return tbCard[1]
		end		
	end
	tbCard = self:getSanDaiYi(tbCardScr)
	if tbCard and next(tbCard) then 
		return tbCard[1]
	end		

	tbCard = self:getShunzi(tbCardScr)
	if tbCard and next(tbCard) then 
		return tbCard[1]
	end	

	tbCard = self:getSanZhang(tbCardScr)
	if tbCard and next(tbCard) then 
		return tbCard[1]
	end		
	tbCard = self:getDuiZi(tbCardScr)
	if tbCard and next(tbCard) then 
		return tbCard[1]
	end			
	tbCard = self:getDanZhang(tbCardScr)
	if tbCard and next(tbCard) then 
		return tbCard[1]
	end		
	if tbZha and tbZha[1] then 
		return tbZha[1]
	end		
end

--指定的牌是否在手牌中是n条
function Rule:cardIsLiang(card,tbCard,n)
	local cardNum = self:getCardNum(card)
	local num = 0
	if not n then 
		n = 2
	end
	for k,v in pairs(tbCard) do 
		if cardNum == self:getCardNum(v) then 
			num = num + 1
		end
	end
	if num >= n then
		return true
	end
	return false
end

--取手中的单牌,ignoreCard放在最后
function Rule:getDanPai(tbCard,ignoreCardNum)
	local tbLiang = {}
	local tbDan = {}
	local tbIgnore = {}
	for j=1,#tbCard do 
		if self:getCardNum(tbCard[j]) ~= ignoreCardNum then 
			if self:cardIsLiang(tbCard[j],tbCard,2) then
				table.insert(tbLiang,tbCard[j])
			else
				table.insert(tbDan,tbCard[j])
			end
		else
			table.insert(tbIgnore,tbCard[j])
		end
	end
	table.sort(tbDan)
	for i=#tbLiang,1,-1 do 
		table.insert(tbDan,tbLiang[i])
	end
	for i=#tbIgnore,1,-1 do 
		table.insert(tbDan,tbIgnore[i])
	end
	return tbDan
end

return Rule